<?php
/**
* @package		Joomla
* @subpackage	Content
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

/* Check to ensure this file is included in Joomla! */
defined( '_JEXEC' ) or die( 'Restricted access' );
$mainframe->registerEvent( 'onAfterContentSave', 'pluginContentJTagsSave' );

	/**
	* Saves tags into database
	* Method is called right after the content is saved
	*
	* @param 	object		A JTableContent object
	* @param 	bool		If the content is just about to be created
	*/
	function pluginContentJTagsSave( &$article, $isNew )
	{
		if (!$isNew)
		{
			$tagsList = JRequest::getVar('tags');
			$tagsList = str_replace(", ", ",", $tagsList);
			$tagsList = str_replace(" ,", ",", $tagsList);

			$tagsArray = explode(',', $tagsList);

			if ($tagsList != "")
			/* check if all tags have more or equal 3 characters */
			foreach ($tagsArray as $tag)
			{
				if (strlen($tag) < 3)
				{
					JError::raiseWarning( '1', 'Tag must contain at least 3 characters! Tags NOT saved.' );
					$error = true;
					break;
				}
			}

			/* if all tags are OK */
			if (!$error)
			{
				$id = JRequest::getVar('id');
				if (!$id)
				{
					$cid = JRequest::getVar( 'cid' , array() , '' , 'array' );
					$id = $cid[0];
				}
				$option = JRequest::getVar('option');

				$query = 'DELETE FROM #__jtags_items WHERE component = \''.$option.'\' and item_id = \''.$id.'\'';
				$db =& JFactory::getDBO();
				$db->setQuery( $query );
				if (!$db->query())
				{
					echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
				}

				/* if tags list is not empty */
				if ($tagsList != "")
				{
					foreach ($tagsArray as $tag)
					{
						$tag_id = getTagId($tag);
						if (!isset($tag_id))
						{
							insertTag($tag);
							$tag_id = getTagId($tag);
						}
						$query = 'INSERT INTO #__jtags_items (`component`, `tag_id`, `item_id`) VALUES (\''.$option.'\', \''.$tag_id.'\', \''.$id.'\')';
						$db->setQuery( $query );
						if (!$db->query())
						{
							echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
						}
					}
				}
			}
		}
	}

	/**
	* Gets tag ID
	*
	* @param 	String		$title		tag name
	* @return 	String		$tagsList	tag id
	*/
	function getTagId($title)
	{
		$query = 'SELECT tag_id FROM #__jtags_tags WHERE name=\''.$title.'\'';
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadResult();
	}

	/**
	* Inserts tag to database
	*
	* @param 	String		$title		tag name
	*/
	function insertTag($title)
	{
		$query = 'INSERT INTO #__jtags_tags (`name`) VALUES (\''.$title.'\')';
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query())
		{
			echo "<script> alert('".$db->getErrorMsg()."'); window. history.go(-1); </script>\n";
		}
	}
?>